package com.wude.guild.imageutil.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.squareup.picasso.Picasso;
import com.wude.guild.imageutil.R;
import com.wude.guild.imageutil.dialog.PhotoDialog;
import com.wude.guild.imageutil.dialog.ShowImageDialog;
import com.wude.guild.imageutil.face.ImageDelFace;
import com.wude.guild.imageutil.face.PhotoFace;
import com.wude.guild.imageutil.face.ShowImagesFace;

/**
 * Created by 唐小妹 on 2017/12/6 0006.
 */

public class MyImageView extends LinearLayout implements View.OnClickListener {
    private Context context;
    private LinearLayout ll_content;
    private ImageView iv_content;
    private ImageView iv_del;
    private ImageDelFace face;
    private Bitmap bitmap;
    private PhotoFace photoFace;
    private ShowImagesFace showImagesFace;
    private String imgUrl;

    public MyImageView(Context context, ImageDelFace face, PhotoFace photoFace, ShowImagesFace showImagesFace) {
        super(context);
        this.context = context;
        this.face = face;
        this.photoFace = photoFace;
        this.showImagesFace = showImagesFace;

        initView();
    }

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.tc_image_view, this);
        ll_content = findViewById(R.id.ll_content);
        iv_content = findViewById(R.id.iv_content);
        iv_del = findViewById(R.id.iv_del);

        iv_del.setOnClickListener(this);
        ll_content.setOnClickListener(this);
    }

    public void setImageBitmap(Bitmap bm) {
        //在这里改变这个imageView的点击事件
        iv_content.setImageBitmap(bm);
        ll_content.setOnClickListener(this);
        iv_del.setVisibility(VISIBLE);
        bitmap = bm;
    }

    public void loadImg(String imgUrl) {
        Picasso.with(context).load(imgUrl).into(iv_content);
        this.imgUrl = imgUrl;
        ll_content.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (R.id.ll_content == id) {
            if (bitmap != null) {
                //上传图片 查看大图
                ShowImageDialog dialog = new ShowImageDialog(context, bitmap);
                dialog.show();
            } else if (imgUrl != null && showImagesFace != null) {
                //网络图片查看大图
                showImagesFace.showImages((int) getTag());
            } else {
                PhotoDialog dialog = new PhotoDialog(context, photoFace);
                dialog.show();
            }
        } else if (R.id.iv_del == id) {
            face.delImg((long) getTag());
        }
    }
}
